package com.geek.javase.ch17.work.tcpwork.tcp07v2;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.CountDownLatch;

/**
 * @Desc 写
 * @Author
 * @Date 2025/11/10
 */
public class WriterThread implements Runnable{

    private Socket socket;
    private CountDownLatch countDownLatch;

    public WriterThread(Socket socket , CountDownLatch countDownLatch){
        this.socket = socket;
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        try (
                PrintWriter pw = new PrintWriter(socket.getOutputStream());
                Scanner scanner = new Scanner(System.in);
        ){
            while(true){
                System.out.println("请说：");
                String str = scanner.nextLine();
                pw.println(str);
                pw.flush();

                if(str.equals("886")){
                    break;
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //计数器 -1 通知客户端可以关闭socket
            countDownLatch.countDown();
        }
    }
}
